/*
**	Anderson Ferminiano
**	contato@andersonferminiano.com -- feel free to contact me for bugs or new implementations.
**	jQuery ScrollPagination
**	28th/March/2011
**	http://andersonferminiano.com/jqueryscrollpagination/
**	You may use this script for free, but keep my credits.
**	Thank you.
*/

(function( $ ){
    $.fn.scrollPagination = function(options) {
        var opts = $.extend($.fn.scrollPagination.defaults, options);
        var target = opts.scrollTarget;
        if (target == null){
            target = obj;
        }
        opts.scrollTarget = target;
        return this.each(function() {
            //  if(opts.isstart == 'start'){
            $.fn.scrollPagination.init($(this), opts);
            //  }

        });
    };
    $.fn.stopScrollPagination = function(){
        return this.each(function() {
            $(this).attr('scrollPagination', 'disabled');
        });
    };

    $.fn.scrollPagination.loadContent = function(obj, opts){
        var target = opts.scrollTarget;
        var mayLoadContent = $(target).scrollTop()+opts.heightOffset >= $(document).height() - $(target).height();
        //根据mayLoadContent 和 lock两个参数进行判断
        if (mayLoadContent && opts.lock){
            if (opts.beforeLoad != null){
                opts.beforeLoad();
            }
            //加载数据的时候把lock设为false
            opts.lock = false;
            $(obj).children().attr('rel', 'loaded');
            $.ajax({
                type: 'POST',
                url: opts.contentPage,
                data: opts.contentData,
                success: function(resp){
                    //加载成功后把lock设为true，可以进行下一次request
                    opts.lock = true;
                    resp = $.parseJSON(resp);
                    if(resp.reqStatus) {
                        var list = resp.data.list;
                        var list_html = '';
                        $.each(list, function (i, item) {
                            list_html += '<li>' +
                                '<a href="' + item.task_url + '"></a>' +
                                '<p>' + item.task_sn + item.subtask_status+ '</p>' +
                                '<div class="con mt5">' +
                                    '<font class="iconfont ml5"><span style="color: #865">任务名称：</span><span>' + item.title + '</span></font>' +
                                '</div>' +
                                '<div class="con mt5">' +
                                '<font class="iconfont ml5"><span style="color: #865">佣金：</span><span>' + item.commission + '</span></font>' +
                                '</div>';
                                if(item.cate_id==4){
                                    list_html += '<div class="con mt5">' +
                                            '<font class="iconfont ml5"><span style="color: #865">完成数量：</span><span>' + item.end_num + '</span></font>' +
                                            '</div>';
                                    if(item.qualification==1){
                                        list_html += '<div class="con mt5">' +
                                            '<font class="iconfont ml5"><span style="color: #865">任务链接：</span><span>' + item.redirect_url + '</span></font>' +
                                            '</div>';
                                        list_html += '<div class="con mt5">' +
                                            '<font class="iconfont ml5"><span style="color: #865">统计链接：</span><span>' + item.count_url + '</span></font>' +
                                            '</div>';
                                    }
                                }
                            list_html += '<div class="con mt5">' +
                                    '<font class="iconfont ml5"><span style="color: #865">状态：</span><span>' + item.task_status + '</span></font>' +
                                    item.rec_sta +
                                 '</div>' +
                                 '<div class="con mt5">' +
                                    '<font class="iconfont ml5"><span style="color: #865">验证意见：</span><span>' + item.suggestion + '</span></font>' +
                                 '</div>' +
                                '</li>';
                        });
                    }
                    $(obj).append(list_html);
                    var objectsRendered = $(obj).children('[rel!=loaded]');

                    if (opts.afterLoad != null){
                        opts.afterLoad(objectsRendered);
                    }
                },
                dataType: 'html'
            });
        }
    };
    $.fn.scrollPagination.init = function(obj, opts){
        var target = opts.scrollTarget;
        $(obj).attr('scrollPagination', 'enabled');
        if(opts.initLoad != null){
            opts.initLoad();
        }
        $(target).scroll(function(event){
            if ($(obj).attr('scrollPagination') == 'enabled'){
                $.fn.scrollPagination.loadContent(obj, opts);
            }
            else {
                event.stopPropagation();
            }
        });
        $.fn.scrollPagination.loadContent(obj, opts);
    };

    $.fn.scrollPagination.defaults = {
        'contentPage' : null,
        'contentData' : {},
        'beforeLoad': null,
        'afterLoad': null	,
        'scrollTarget': null,
        'heightOffset': 0,
        //添加lock机制，如果数据加载完了，则lock为true，可以加载下一组数据，如果数据没有加载完，则lock为false，等到数据加载完成了为true。  
        'lock':true,
        'initLoad':null//插件初始化时调用的方法
    };
})( jQuery );